<template>
  <EditOrViewTable :table-data="tableData" :type="type" :setting="setting" />
</template>
<script>
import EditOrViewTable from './editOrViewTable'
import { validIDCard } from '@/utils/validate'
export default {
  name: 'PartnershipTable',
  components: {
    EditOrViewTable
  },
  props: {
    tableData: {
      type: Array,
      default: () => []
    },
    type: {
      type: String,
      default: 'view'
    },
    title: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      setting: [
        {
          prop: 'name',
          label: '合伙人姓名',
          required: this.type === 'edit',
          minWidth: 140,
          editRender: (h, row) => {
            return h('el-input', {
              props: {
                value: row.name,
                placeholder: '请输入',
                clearable: true
              },
              attrs: {
                placeholder: '请输入',
                clearable: true
              },
              on: {
                input: v => {
                  this.$set(row, 'name', v)
                }
              },
              nativeOn: {
                click: e => e.stopPropagation()
              }
            })
          }
        },
        {
          prop: 'tel',
          label: '合伙人电话',
          required: this.type === 'edit',
          minWidth: 140,
          editRender: (h, row) => {
            return h('el-input', {
              props: {
                value: row.tel,
                placeholder: '请输入',
                clearable: true
              },
              attrs: {
                placeholder: '请输入',
                clearable: true
              },
              on: {
                input: v => {
                  this.$set(row, 'tel', v)
                }
              },
              nativeOn: {
                click: e => e.stopPropagation()
              }
            })
          }
        },
        {
          prop: 'credentialNo',
          label: '合伙人身份证号',
          required: this.type === 'edit',
          minWidth: 140,
          editRender: (h, row) => {
            return h('el-input', {
              props: {
                value: row.credentialNo,
                placeholder: '请输入',
                clearable: true
              },
              attrs: {
                placeholder: '请输入',
                clearable: true
              },
              on: {
                input: v => {
                  this.$set(row, 'credentialNo', v)
                }
              },
              nativeOn: {
                click: e => e.stopPropagation()
              }
            })
          }
        }
      ]
    }
  },
  methods: {
    validate() {
      const regPhone = /^1\d{10}|([0-9]{3,4}-)?[0-9]{7,8}$/
      const requiredKeys = {
        name: '合伙人姓名',
        tel: '合伙人电话',
        credentialNo: '合伙人身份证号'
      }
      const errMsgs = []
      for (let i = 0; i < this.tableData.length; i++) {
        const item = this.tableData[i]
        // 必填
        for (const [k, msg] of Object.entries(requiredKeys)) {
          const v = item[k]
          if (v === undefined || v === null || v === '') {
            errMsgs.push(`【${this.title}】第${i + 1}行【${msg}】必填(选)!`)
          }
        }

        if (!regPhone.test(item.tel)) {
          errMsgs.push(`请检查【${this.title}】第${i + 1}行合伙人电话格式!`)
        }
        // 身份证
        if (!validIDCard(item.credentialNo)) {
          errMsgs.push(`请检查【${this.title}】第${i + 1}行合伙人身份证号格式!`)
        }
      }
      if (errMsgs.length > 0) {
        this.$message({
          type: 'warning',
          message: errMsgs.join('<br>'),
          dangerouslyUseHTMLString: true
        })
        return Promise.reject(errMsgs)
      }
      return Promise.resolve(this.tableData)
    }
  }
}
</script>
